import { _decorator, Component, EventKeyboard, Input, input, KeyCode, Node } from 'cc';
import { EventManager, MyEvent } from './EventManager';

const { ccclass, property } = _decorator;

export enum KEYCODE {
    A,
    D,
    W,
    S,
}

export enum DIR {
    ROW,
    COL,
}

@ccclass('InputManager')
export class InputManager extends Component {

    ArrRowKeyCode: KEYCODE[] = [];
    ArrCloKeyCode: KEYCODE[] = [];

    protected onLoad(): void {
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
    }

    onKeyDown(event: EventKeyboard) {
        switch (event.keyCode) {
            case KeyCode.KEY_A:
                this.pushKeyCode(this.ArrRowKeyCode, KEYCODE.A);
                break;
            case KeyCode.KEY_D:
                console.log(2);
                this.pushKeyCode(this.ArrRowKeyCode, KEYCODE.D);
                break;
            case KeyCode.KEY_W:
                this.pushKeyCode(this.ArrCloKeyCode, KEYCODE.W);
                break;
            case KeyCode.KEY_S:
                this.pushKeyCode(this.ArrCloKeyCode, KEYCODE.S);
                break;
        }
        this.emit();
    }

    onKeyUp(event: EventKeyboard) {
        switch (event.keyCode) {
            case KeyCode.KEY_A:
                this.removeKeyCode(this.ArrRowKeyCode, KEYCODE.A);
                break;
            case KeyCode.KEY_D:
                this.removeKeyCode(this.ArrRowKeyCode, KEYCODE.D);
                break;
            case KeyCode.KEY_W:
                this.removeKeyCode(this.ArrCloKeyCode, KEYCODE.W);
                break;
            case KeyCode.KEY_S:
                this.removeKeyCode(this.ArrCloKeyCode, KEYCODE.S);
                break;
        }
        this.emit();
    }

    pushKeyCode(arrKeyCode: KEYCODE[], keyCode: KEYCODE) {
        if (arrKeyCode.findIndex(e => e == keyCode) == -1) {
            arrKeyCode.push(keyCode);
        }
    }

    removeKeyCode(arrKeyCode: KEYCODE[], keyCode: KEYCODE) {
        const index: number = arrKeyCode.findIndex(e => e == keyCode);
        if (index != -1) {
            arrKeyCode.splice(index, 1);
        }
    }

    getKeyCode(arrKeyCode: KEYCODE[]) {
        if (arrKeyCode.length == 0) return -1;
        return arrKeyCode[arrKeyCode.length - 1];
    }

    emit() {
        const row = this.getKeyCode(this.ArrRowKeyCode);
        if (row == KEYCODE.A) {
            EventManager.Scene.emit(MyEvent.MOVE, DIR.ROW, -1);
        } else if (row == KEYCODE.D) {
            EventManager.Scene.emit(MyEvent.MOVE, DIR.ROW, 1);
        } else {
            EventManager.Scene.emit(MyEvent.MOVE, DIR.ROW, 0);
        }
        const col = this.getKeyCode(this.ArrCloKeyCode);
        if (col == KEYCODE.S) {
            EventManager.Scene.emit(MyEvent.MOVE, DIR.COL, -1);
        } else if (col == KEYCODE.W) {
            EventManager.Scene.emit(MyEvent.MOVE, DIR.COL, 1);
        } else {
            EventManager.Scene.emit(MyEvent.MOVE, DIR.COL, 0);
        }
    }

}


